<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2016/4/22 0022
 * Time: 上午 9:56
 */
namespace Api\Model;
use Think\Model;

class BlogModel extends Model{
    private $_category_cache = 'blog_categorys_cache';

    public function comments($options = array(), $limit = 0, $page_size = 10, $fields = '*'){

        if(empty($options) || !isset($options['aid'])){
            return array();
        }else{
            $options['aid'] = intval($options['aid']);
        }

        $limitStr = '';
        $data = array();
        if(intval($limit) > 0){
            $limitStr = $limit;
        }else{
            $count = $this->where($options)->count();
            $Page = new \Think\Page($count, max($page_size,1));
            $limitStr = $Page->firstRow.','.$Page->listRows;
            $data['page'] = $Page->show();
        }

        $fields = format_fields($fields,'A');

        $where = array();
        foreach($options as $k=>$v){
            $where['A.'.$k] = $v;
        }

        $data['data'] = D('BlogArticleComments')->alias('A')->field($fields.',B.nickname,B.avatar')
            ->join(C("DB_PREFIX").'user B ON A.comment_uid = B.id','left')
            ->where($where)->order('id desc')->limit($limitStr)->select();

        return $data;
    }

    //记录访客
    public function record($uid, $blog_id){
        $uid = intval($uid);
        $blog_id = intval($blog_id);
        if(empty($uid) || empty($blog_id)){
            return false;
        }

        $blog = M('Blog')->find($blog_id);
        if(empty($blog) || $blog['uid'] == $uid){
            return false;
        }

        $cache_tag = 'cache_blog_record_'.$blog_id.'_'.$uid;
        $cache = S($cache_tag);
        if(empty($cache)){
            $has = M('BlogRecords')->where(array('uid'=>$uid,'blog_id'=>$blog_id))->count();

            if($has > 0){
                $where = array(
                    'uid'   =>  $uid,
                    'blog_id'   =>  $blog_id,
                );
                M('BlogRecords')->where($where)->setField('create_time', time());
            }else{
                $data = array();
                $data['uid'] = $uid;
                $data['blog_id'] = $blog_id;
                $data['create_time'] = time();
                $data['ip'] = $_SERVER['REMOTE_ADDR'];
                M("BlogRecords")->data($data)->add();
            }

            $time_stamp = strtotime(date('Y-m-d').'23:59:59') - time();
            S($cache_tag, time(),$time_stamp);
        }
    }

    //读取博客的文章
    public function getArticles($options = array(), $limit = 0, $page_size = 20, $order, $fields = '*'){

        $where = array();
        $where['status'] = 1;

        if(!empty($options)){
            foreach ($options as $k=>$v){
                $where[$k] = $v;
            }
        }
        if(empty($order)){
            $order = 'is_top desc,is_recommend desc,id desc';
        }

        $data = array();
        if(intval($limit) < 1){
            $count = M('BlogArticle')->where($where)->count();
            $Page = new \Think\Page($count, $page_size);
            $Page->setConfig('theme', '%FIRST% %LINK_PAGE% %END%');
            $limit = $Page->firstRow.','.$Page->listRows;
            $data['page'] = $Page->show();
        }

        $fields = format_fields($fields);

        $data['data'] = M('BlogArticle')->field($fields)->where($where)->order($order)->limit($limit)->select();


        $cats = S($this->_category_cache);
        foreach ($data['data'] as $k=>$v){
            if(isset($cats[$v['cat_id']])){
                $data['data'][$k]['cat_name'] = $cats[$v['cat_id']];
            }

            $blog = M('Blog')->field('uid,name')->find($v['blog_id']);

            if(!empty($blog)){
                $data['data'][$k]['blog_name'] = $blog['name'];
                $user = M('User')->field('nickname')->find($blog['uid']);
                $data['data'][$k]['username'] = $user['nickname'];
            }

            $data['data'][$k]['desc'] = htmlspecialchars_decode(stripslashes($v['desc']));
        }
        return $data;
    }

    //根据userid获取到指定用户发布的博文
    public function getArticleByUser($options = array(), $limit = 0, $page_size = 20){
        if(empty($options) || !isset($options['userid'])){
            return array();
        }

        $blog = $this->alias('A')->field('A.id,A.uid,A.name,B.nickname,B.avatar')
            ->join(C('DB_PREFIX').'user B ON A.uid = B.id','left')
            ->where(array('A.uid'=>$options['userid']))->find();

        if(empty($blog)){
            return array();
        }

        unset($options['userid']);
        $where = array();
        $data = array();
        $where['status'] = 1; //正常状态
        $where['blog_id'] = $blog['id'];
        if(!empty($options)){
            foreach($options as $k=>$v){
                $where[$k] = $v;
            }
        }

        if(intval($limit) < 1){
            $count = M('BlogArticle')->where($where)->count();
            $Page = new \Think\Page($count, $page_size);
            $Page->setConfig('theme', '%FIRST% %LINK_PAGE% %END%');
            $limit = $Page->firstRow.','.$Page->listRows;
            $data['page'] = $Page->show();
        }

        $data['data'] = M('BlogArticle')->where($where)->order('id desc')->limit($limit)->select();
        $cats = S($this->_category_cache);

        foreach ($data['data'] as $k=>$v){
            if(isset($cats[$v['cat_id']])){
                $data['data'][$k]['cat_name'] = $cats[$v['cat_id']];
            }
            $data['data'][$k]['blog_name'] = $blog['name'];
            $data['data'][$k]['username'] = $blog['nickname'];
            $data['data'][$k]['avatar'] = $blog['avatar'];
            $data['data'][$k]['uid'] = $blog['uid'];
            $data['data'][$k]['desc'] = htmlspecialchars_decode(stripslashes($v['desc']));
        }

        return $data;
    }

    //获取公告
    public function adv($blog_id = 0){
        if(empty($blog_id)){
            return array();
        }

        $adv = M('BlogAdv')->where(array('blog_id'=>$blog_id))->find($blog_id);
        return $adv;
    }
}
